package com.xxxx.system.controller.admin;


import com.xxxx.server.dto.PageDto;
import com.xxxx.server.dto.ResponseDto;
import com.xxxx.server.dto.RoleDto;
import com.xxxx.server.service.RoleService;
import com.xxxx.server.util.ValidatorUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/admin/role")
public class RoleController {

    public static final String BUSINESS_NAME = "角色";

    @Resource
    private RoleService roleService;

    @PostMapping("/list")
    public ResponseDto list(@RequestBody PageDto pageDto) {
        ResponseDto<PageDto> responseDto = new ResponseDto<>();

        roleService.list(pageDto);
        responseDto.setContent(pageDto);
        return responseDto;
    }

    @PostMapping("/save")
    public ResponseDto save(@RequestBody RoleDto roleDto) {
        //  保存校验
        ValidatorUtil.require(roleDto.getName(), "角色");
        ValidatorUtil.length(roleDto.getName(), "角色", 1, 50);
        ValidatorUtil.require(roleDto.getDesc(), "描述");
        ValidatorUtil.length(roleDto.getDesc(), "描述", 1, 100);

        ResponseDto<RoleDto> responseDto = new ResponseDto<>();

        roleService.save(roleDto);
        responseDto.setContent(roleDto);
        return responseDto;
    }

    @DeleteMapping("/delete/{id}")
    public ResponseDto delete(@PathVariable String id) {
        ResponseDto<RoleDto> responseDto = new ResponseDto<>();

        roleService.delete(id);
        return responseDto;
    }

    /**
     * 保存角色资源关联
     * @param roleDto
     * @return
     */
    @PostMapping("/save-resource")
    public ResponseDto saveResource(@RequestBody RoleDto roleDto) {
        ResponseDto<RoleDto> responseDto = new ResponseDto<>();
        roleService.saveResource(roleDto);
        responseDto.setContent(roleDto);
        return responseDto;
    }

    /**
     * 根据角色id查询其关联的资源
     * @param id
     * @return
     */
    @GetMapping("/list-role-resource/{id}")
    public ResponseDto listRoleResource(@PathVariable String id) {
        ResponseDto<List<String>> responseDto = new ResponseDto<>();
        List<String> resourceIdList = roleService.listRoleResource(id);
        responseDto.setContent(resourceIdList);
        return responseDto;
    }

    /**
     * 保存角色用户关联
     * @param roleDto
     * @return
     */
    @PostMapping("/save-user")
    public ResponseDto saveUser(@RequestBody RoleDto roleDto) {
        ResponseDto<RoleDto> responseDto = new ResponseDto<>();
        roleService.saveUser(roleDto);
        responseDto.setContent(roleDto);
        return responseDto;
    }

    @GetMapping("/list-role-user/{roleId}")
    public ResponseDto listRoleUser(@PathVariable String roleId) {
        ResponseDto<List<String>> responseDto = new ResponseDto<>();
        List<String> userIdList = roleService.listRoleUser(roleId);
        responseDto.setContent(userIdList);
        return responseDto;
    }
}
